//
//  ContentHeaderView.m
//  7.13苏宁框架
//
//  Created by app01 on 15/7/18.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "ContentHeaderView.h"
#import "Common.h"
#import "TopicModel.h"

@interface ContentHeaderView ()

{
    CGFloat descDeltaH;
    CGFloat originalDeltaH;
    CGFloat originalSelfH;
    CGRect originalSelfF;
    CGRect riginalDeltaF;
}


@end


@implementation ContentHeaderView


-(void)awakeFromNib{
    _selectBtn = _btn;
    
    
    
    //描述在画时宽高
    CGRect descF = _contentDescription.frame;
    riginalDeltaF = descF;
    originalDeltaH = descF.size.height;
    
    CGRect selfF = self.frame;
    originalSelfF = selfF;
    originalSelfH = selfF.size.height;
}

-(void)drawRect:(CGRect)rect{
    
    CGContextRef ref = UIGraphicsGetCurrentContext();
    [[UIColor lightGrayColor]set];
    CGContextSetLineWidth(ref, 0.8);
    CGContextMoveToPoint(ref, _contentImage.left, _contentImage.bottom);
    CGContextAddLineToPoint(ref, _contentDescription.left, _contentDescription.bottom);
    CGContextAddLineToPoint(ref, _contentDescription.right, _contentDescription.bottom);
    CGContextAddLineToPoint(ref, _contentImage.right, _contentImage.bottom);
    CGContextStrokePath(ref);
}

-(void)layoutSubviews{
    
    [_contentImage setImageWithURL:[NSURL URLWithString:_topicModel.icon]];
    _contentTitle.text = [NSString stringWithFormat:@"# %@ #",_topicModel.title];
    
    CGSize size = [_topicModel.descrip sizeWithFont:_contentDescription.font constrainedToSize:CGSizeMake(_contentDescription.frame.size.width, MAXFLOAT) ];
    CGFloat descH = size.height+10;
    //实际宽高与画的差
    descDeltaH = descH - originalDeltaH;
    
    if (_contentDescription.tag == 0) {
        
        [_arrowBtn setBackgroundImage:[UIImage imageNamed:@"up.png"] forState:UIControlStateNormal];
        
        _contentDescription.numberOfLines = 2;
        //设置整体的高度
        self.frame = originalSelfF;
        
        _contentDescription.frame = riginalDeltaF;
        
        
    }else{
        [_arrowBtn setBackgroundImage:[UIImage imageNamed:@"down.png"] forState:UIControlStateNormal];
        
        _contentDescription.numberOfLines = 0;
        
        CGRect desF = _contentDescription.frame;
        // 描述的高度
        desF.size.height = originalDeltaH + descDeltaH ;
        //NSLog(@"%lf",desF.size.height);
        
        // 6.设置整体的高度
        CGRect selfF = self.frame;
        selfF.size.height =originalSelfH + descDeltaH;


    }
    
    _contentDescription.text =_topicModel.descrip;
    
    
}




- (IBAction)setlectBtnAction:(id)sender {
    if ([self.delegate respondsToSelector:@selector(click:)]) {
        [self.delegate click:sender];
    }
}

- (IBAction)changeSize:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(clickChangeSize:height:)]) {
        
        [self.delegate clickChangeSize:_contentDescription height:descDeltaH];
        [self setNeedsDisplay];
    }
}
@end
